# ifndef __SLTlist__H__
# define __SLTlsit__H__

#include <cstdio>
#include <cstdlib>

typedef int SLTDataType;
struct SListNode
{
	SLTDataType data;
	struct SListNode* next;
};

typedef struct SListNode SLTNode;

SLTNode* MallocNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode == nullptr)
		exit(-1);
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}

void SLTPushFront(SLTNode** plist, SLTDataType x)
{
	SLTNode* newnode = MallocNode(x);
	newnode->next = *plist;
	*plist = newnode;
}

void SLTPushBack(SLTNode** plist, SLTDataType x)
{
	SLTNode* newnode = MallocNode(x);
	if (*plist == NULL)
	{
		*plist = newnode;
	}
	else if ((*plist)->next == NULL)
	{
		(*plist)->next = newnode;
	}
	else
	{
		SLTNode* cur = *plist;
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = newnode;
	}
}

void SLTPopFront(SLTNode** plist)
{
	SLTNode* next = (*plist)->next;
	free(*plist);
	*plist = next;
}

void SLTPopBack(SLTNode** plist)
{
	if ((*plist)->next == NULL)
	{
		free(*plist);
		*plist = NULL;
		return;
	}
	SLTNode* cur = *plist;
	SLTNode* prev = NULL;
	while (cur->next != NULL)
	{
		prev = cur;
		cur = cur->next;
	}
	free(cur);
	prev->next = NULL;
}

void SLTPrint(SLTNode* plist)
{
	SLTNode* cur = plist;
	while (cur)
	{

		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

SLTNode* SLTFind(SLTNode* plist, SLTDataType x)
{
	SLTNode* cur = plist;
	while (cur)
	{
		if (x == cur->data)
		{
			return cur;
		}
		cur = cur->next;
	}
	return nullptr;
}



# endif